package com.nc.delivery_project.server.dao;

import com.nc.delivery_project.server.domain.Order;
import com.nc.delivery_project.server.domain.Payment;
import com.nc.delivery_project.server.domain.User;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created with IntelliJ IDEA.
* User: Natalie D.
* Date: 12/28/14
* Time: 3:06 PM
*/
public class OrderDao extends AbstractDao<Order>
{

    protected final static String ENTITY_NAME = Order.class.getSimpleName();


    public OrderDao() {
        super(ENTITY_NAME);
    }
    public OrderDao(Connection connection) throws SQLException {
        super(connection, ENTITY_NAME);
    }

    @Override
    protected void prepareInsertStatement(CallableStatement statement, Order object) throws SQLException {
       // statement.setInt(1, object.getId());
        statement.registerOutParameter(1, Types.NUMERIC);
        statement.setString(2,object.getStartDate());
        statement.setString(3,object.getDeliveryDate());
        statement.setString(4, object.getComment());
        statement.setInt(5, object.getSender().getId());
        statement.setInt(6, object.getReceiver().getId());
      //  statement.setString(7, object.getStatus());
    }

    @Override
    protected void prepareUpdateStatement(CallableStatement statement, Order object) throws SQLException {
        statement.setInt(1, object.getId());
        statement.setString(2,object.getStartDate());
        statement.setString(3, object.getDeliveryDate());
        statement.setString(4,object.getComment());
//        statement.setInt(5,object.getSender().getId());
//        statement.setInt(6,object.getReceiver().getId());
        statement.setString(5,object.getStatus());
    }


    @Override
    protected List<Order> parseResultSet(ResultSet rs) throws SQLException {
        List<Order> orders = new ArrayList<>();
        while (rs.next())
        {
            Long id=rs.getLong("ORDER_ID");
            String startDate=rs.getString("START_DATE");
            String deliveryDate=rs.getString("DELIVERY_DATE");
            String comment=rs.getString("COMMENTS");
            User sender= new User(rs.getInt("SENDER"));
            User receiver= new User(rs.getInt("RECEIVER"));
            String status = rs.getString("STATUS");
            Payment payment = new Payment(rs.getLong("PAYMENT"));
            orders.add(new Order(id,sender,receiver,startDate,deliveryDate,comment,status,payment));
        }

        return orders;
    }

    protected String getReadBySenderIdQuery() {
        return queryManager.getQuery(ENTITY_NAME, "read_by_sender_id");
    }
    protected String getReadByReceiverIdQuery() {
        return queryManager.getQuery(ENTITY_NAME, "read_by_receiver_id");
    }


    public List<Order> readBySenderId(int id)
    {
        try
        {
            PreparedStatement statement = connection.prepareStatement(getReadBySenderIdQuery());
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            return parseResultSet(rs);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public List<Order> readByReceiverId(int id)
    {
        try
        {
            PreparedStatement statement = connection.prepareStatement(getReadByReceiverIdQuery());
            statement.setInt(1, id);
            ResultSet rs = statement.executeQuery();
            return parseResultSet(rs);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public int create(Order obj) throws SQLException {
        return super.create(obj, 1);
    }
}
